package com.scs.application.modules.fsd.controller;

import com.alibaba.fastjson.JSONObject;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.service.BaoYiService;
import com.scs.application.modules.fsd.vo.SPDResponseResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 */
@RestController
@RequestMapping("/baoyi")
public class BaoYiController extends IntelliAPIController {

    @Autowired
    private BaoYiService baoYiService;
    public RestResult netTest() {
        return RestResult.ok("网络连接正常--baoyi");
    }

    /**
     * 2box扫码查询耗材信息
     * @param config
     * @param codes
     * @return
     */
    @GetMapping(value = "/box/scan")
    public RestResult getScanResult(@RequestHeader("Intellic-configuration") String config,@RequestParam("barCodes") String codes) {
        List<String> codeList = new ArrayList<>(Arrays.asList((StringUtils.strip(codes, "[]").replace("\"","").split(","))));
        return RestResult.ok("扫描成功", baoYiService.getScanResult(JSONObject.parseObject(config).getString("stationCode"), codeList));
    }

    /**
     * 2box低值消耗
     *
     * @return
     */
    @PostMapping(value = "/box/consume")
    public RestResult lowConsume(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        String rfids =map.get("barCodes");
        List<String> rfidList = new ArrayList<>(Arrays.asList((StringUtils.strip(rfids, "[]").replace("\"","").split(","))));
        SPDResponseResult responseResult = baoYiService.lowConsume(JSONObject.parseObject(config).getString("stationCode"), rfidList);
        if (responseResult.getCode() == 0)
            return RestResult.ok("消耗成功");
        return RestResult.error("消耗失败", responseResult.getMsg());
    }

    /**
     * 2box库存
     * @param config
     * @return
     */
    @GetMapping(value = "/box/getlowstockquery")
    public RestResult getLowStockByStationCode(@RequestHeader("Intellic-configuration") String config)
    {
        return RestResult.ok("查询成功", baoYiService.getLowStockByStationCode(JSONObject.parseObject(config).getString("stationCode")));
    }

    /**
     * 消耗信息
     * @param config
     * @return
     */
    @GetMapping(value = "/box/getconsumequery")
    public RestResult getConsumeStockByStationCode(@RequestHeader("Intellic-configuration") String config)
    {
        return RestResult.ok("查询成功", baoYiService.getConsumeStockByStationCode(JSONObject.parseObject(config).getString("stationCode")));
    }

}
